package com.pkg;

import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.HashMap;
import java.util.Map;

/**
 * 由A加密数据发送给B
 */
public class A2B extends Codec {
    /**
     * B的公钥
     *
     * @return
     */
    @Override
    protected String getPublicKey() {
        return "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCcfpjcNtyeSRRHxaT7wZtM+esk9lGIwuahp+OT" +
                "F3JBfG3lU+GLMiXgrJOoz1U1as0oUGRdoNrYG7qg0j9IXhazZIdYdlZLeSppZe+xjEl7ytA/dHbw" +
                "FgJe12PkiCaO/cVuYvJaGFksjMMFh4ZWTONp2sW0l0wl3A11xQry5VkS3wIDAQAB";
    }

    /**
     * A的私钥
     *
     * @return
     */
    @Override
    protected String getPrivateKey() {
        return "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIkRri31AgLsNaw1eb1ojm6wAinY" +
                "sUapGBVxXMIdkURn4AA7iD9JHdsm7jUtjEQ4fpFUqgf2bdHy9fSMsz1wN2+EY3K/QXxOKUUKYA9L" +
                "6Jd7IVICmbtHzD2nSAIH+cGZijJUDkWyiKGfMBbRyyf6V0bNCCi4qFIAiCmonOpyBTKVAgMBAAEC" +
                "gYBzeX2wIfUr2SvuhK6nL2qvPYDVGEVhLhuoFPT8ZER5yLkh+QVzq3mHo2iTTGgbIS0f6p/ETYt/" +
                "kJw9BjwKynsoLVr32DKvnOnOiaq0PutCRBac40VZIZT+EaQsN1VRkfhNdjCmKcZT7K7t5Os7CMh4" +
                "H470HfPmYDGP7GB5/ab7AQJBANbbJA87keE7YLxrZ+W6FoELUUTklkX8xD/v9D7jSWz8T+pw8DC7" +
                "mAR1BwXn4dvuvF5/3SraHBicFIHOU1DHdEUCQQCjUTMYDu4nvlc0JsbYyLIN8gSNjM27Nj46Px5U" +
                "2TlcFKp21B6KvPfcHPUuwUctBoRsKaciH5LJj8GzdpgHXTIRAkApHUQ4OgD/ZbpsM+1s04BM71p8" +
                "4XU0LVtslMFba2j4lwu/5B7CZJOBftd6NNZOnM+lEbjD43ru5qcx88aTtac1AkEAkkJyA6W7WxR3" +
                "fFkN069I5nxL34it5m9Zo0hLnak8WV6Ymvq0r4M0Zof4e/rUB1rC1SsoZsLn7mM0EcivtkicQQJA" +
                "Q7R3EEE2fgopXoUqsSbF9dZqa73pJYKJCfaEbowOT3kauttDPA8GmBhisgDbkYyMyCAM3Nl8mmMg" +
                "H/PyeZ0PYw==";
    }

    public static Map<String, Object> initKey() throws Exception {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        keyPairGenerator.initialize(1024, random);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();

        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();


        Map<String, Object> map = new HashMap<String, Object>();
        map.put(PUBLIC_KEY, publicKey);
        map.put(PRIVATE_KEY, privateKey);
        return map;

    }

    final static String PUBLIC_KEY = "PUBLIC_KEY";
    final static String PRIVATE_KEY = "PRIVATE_KEY";

    public static void createKey() throws Exception {
        A2B codec = new A2B();

        Map<String, Object> keyMap = initKey();

        Key pukey = (Key) keyMap.get(PUBLIC_KEY);
        Key prkey = (Key) keyMap.get(PRIVATE_KEY);

        System.out.println(codec.encryptBASE64(pukey.getEncoded()));
        System.out.println(codec.encryptBASE64(prkey.getEncoded()));
    }

    public static void main(String[] args) throws Exception {
        A2B codec = new A2B();
        Map<String, String> m1 = codec.encode("中国");
        String a = m1.get("a");
        String b = m1.get("b");
        System.out.println("a=" + a);
        System.out.println("b=" + b);

    }
}
